/**
 * 
 */
package com.brewdroid.db.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.brewdroid.db.DatabaseHelper;
import com.brewdroid.enumerators.MaltFormat;
import com.brewdroid.model.Malt;

/**
 * @author meeusen
 *
 */
public class MaltDAO {

	private static final String TABLE="malt";
	private static final String NULL_COL="name";
	private static final String MALT_SELECT_BY_ID="select * from malt where _id=?";
	private static final String MALT_SELECT_ALL="select * from malt order by name";
	private static final String MALT_SELECT_ID="select _id from malt m where m.description=? and m.name=? and m.lovibond=? and m.format=?";
	private static final String MALT_SELECT_BY_NAME="select * from malt where name=?";
	
	public static Malt getMaltByName(DatabaseHelper db, String name)
	{
		Malt m = null;
		Cursor c = db.getReadableDatabase().rawQuery(MALT_SELECT_BY_NAME, new String[]{name});
		if(c.moveToFirst())
			m=makeMaltFromCursor(c);
		
		return m;
	}
	
	
	public static Malt getMaltById(DatabaseHelper db, int id) {
		Cursor c = db.getReadableDatabase().rawQuery(MALT_SELECT_BY_ID, new String[]{Integer.toString(id)});
		c.moveToFirst();
		Malt m = makeMaltFromCursor(c);
		
		return m;
	}

	public static ArrayList<Malt> getMalts(DatabaseHelper db)
	{
		ArrayList<Malt> malts = new ArrayList<Malt>();
		Cursor c = db.getReadableDatabase().rawQuery(MALT_SELECT_ALL, new String[]{});
		if(c.moveToNext())
		{
			do{
				Malt m = makeMaltFromCursor(c);
				malts.add(m);
				
			}while(c.moveToNext());		
		}
		return malts;
	}
	
	public static ArrayList<String> getMaltStrings(DatabaseHelper db)
	{
		ArrayList<String> maltStrings = new ArrayList<String>();
		for(Malt i: getMalts(db)){
			maltStrings.add(i.getName());
		}
		return maltStrings;
	}
	
	public static void addMalt(DatabaseHelper db, Malt malt)
	{
		if(getId(db, malt)==0)		//only add new malts
		{
			ContentValues cv = new ContentValues();
			cv.put("description", malt.getDescription());
			cv.put("name", malt.getName());
			cv.put("lovibond", Integer.toString(malt.getLovibond()));
			cv.put("format", malt.getFormat().toString().toLowerCase());
			
			db.getWritableDatabase().insert(TABLE, NULL_COL,cv);
		}

	}
	
	public static int getId(DatabaseHelper db, Malt malt)
	{
		int x = 0;
		Cursor c = db.getReadableDatabase().rawQuery(MALT_SELECT_ID, new String[]{
																					malt.getDescription(),
																					malt.getName(),
																					Integer.toString(malt.getLovibond()),
																					malt.getFormat().toString().toLowerCase()
		});
		if(c.moveToFirst())
			x=c.getInt(0);
		
		return x;
		
	}
	
	private static Malt makeMaltFromCursor(Cursor c) {
		Malt m = new Malt();
		m.setDescription(c.getString(1));
		m.setName(c.getString(2));
		m.setLovibond(c.getInt(3));
		
		String format = c.getString(4);
		if(format.equalsIgnoreCase("grain"))
			m.setFormat(MaltFormat.GRAIN);
		else if (format.equalsIgnoreCase("extract"))
			m.setFormat(MaltFormat.EXTRACT);
		
		return m;
	}

}
